package com.ztewatch.wearcontacts.view;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Groups;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ztewatch.wearcontacts.R;

public class ContactSelectCursorAdapter extends CursorAdapter {
	private Context mContext = null;
	private LayoutInflater mInflater;
	private ArrayList<Long> contactIds;// = new ArrayList<Long>();
	private GroupMemerListhener memerListhener;

	public interface GroupMemerListhener {
		void onCheckChange();
	}

	public void setGroupMemerListhener(GroupMemerListhener listhener) {
		memerListhener = listhener;
	}

	public ContactSelectCursorAdapter(Context context, Cursor c) {
		super(context, c);
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setInitSelectContactIds(ArrayList<Long> ids) {
		contactIds = ids;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return mInflater.inflate(R.layout.select_grid_item, parent, false);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final TextView name_tv = (TextView) view.findViewById(R.id.item_text);
		final ImageView photo = (ImageView) view.findViewById(R.id.item_image);
		final CheckBox checkBox = (CheckBox) view
				.findViewById(R.id.item_checkbox);
		if (cursor != null) {
			int index = cursor.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY);
			int id_index = cursor.getColumnIndex(Contacts._ID);

			if (index != -1 && id_index != -1) {
				String name = cursor.getString(index);
				final long id = cursor.getLong(id_index);
				name_tv.setText(name);
				// if (context.getResources().getString(R.string.history_str)
				// .equals(name)
				// && id == 0) {
				// checkBox.setVisibility(View.INVISIBLE);
				// name_tv.setEnabled(false);
				// photo.setEnabled(false);
				// }else{
				// name_tv.setEnabled(true);
				// photo.setEnabled(true);
				// }
				if (contactIds.contains(id)) {
					checkBox.setChecked(true);
				}
				checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							if (id != 0 && !contactIds.contains(id)) {
								contactIds.add(id);
							}
						} else {
							if (id != 0 && contactIds.contains(id)) {
								contactIds.remove(id);
							}
						}
						if (memerListhener != null) {
							memerListhener.onCheckChange();
						}
					}
				});
			} else {
				int index1 = cursor.getColumnIndex(Groups.TITLE);
				if (index1 != -1) {
					String name = cursor.getString(index1);
					name_tv.setText(name);
				}
				// name_tv.setEnabled(true);
				// photo.setEnabled(true);
			}
			bindPhoto(photo, cursor);
		}
	}

	private void bindPhoto(ImageView photo, Cursor cursor) {
		long photoId = 0;
		int index = cursor.getColumnIndex(Contacts.PHOTO_ID);
		if (index != -1) {
			if (!cursor.isNull(index)) {
				photoId = cursor.getLong(index);
			}
		}
		if (photoId != 0) {
			int id_index = cursor.getColumnIndex(Contacts._ID);
			if (id_index != -1) {
				Long contactID = cursor.getLong(id_index);
				Uri uri = ContentUris.withAppendedId(
						ContactsContract.Contacts.CONTENT_URI, contactID);
				InputStream input = ContactsContract.Contacts
						.openContactPhotoInputStream(
								mContext.getContentResolver(), uri);
				photo.setImageBitmap(BitmapFactory.decodeStream(input));
			}
		} else {
			photo.setImageResource(R.drawable.ic_address_portrait);
		}
	}

	public List<String> getLookUpKeys() {
		List<String> list = new ArrayList<String>();
		for (int index = 0; index < getCount(); index++) {
			Cursor cursor = (Cursor) getItem(index);
			if (cursor == null) {
				continue;
			}
			int lookup_key_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
			if (lookup_key_index != -1) {
				String lookupKey = cursor.getString(lookup_key_index);
				list.add(lookupKey);
			}
		}
		return list;
	}
}
